import os

import yaml
from metadrive.engine import AssetLoader
from metadrive.policy.replay_policy import ReplayEgoCarPolicy

from policy import MyTakeoverPolicy

my_global_config = None


# 读取 YAML 配置文件
def load_config(config_file):
    with open(config_file, "r", encoding="utf-8") as file:
        config = yaml.safe_load(file)

    # 获取动态路径
    nuplan_data = AssetLoader.file_path(AssetLoader.asset_path, "nuplan", unix_style=False)
    env_ego_policy = MyTakeoverPolicy

    config['env']['base_config']['data_directory'] = nuplan_data
    config['env']['base_config']['agent_policy'] = env_ego_policy
    return config


def init_config(config_file):
    global my_global_config
    my_global_config = load_config(config_file)


def get_my_global_config():
    # 获取当前脚本所在的目录
    current_dir = os.path.dirname(os.path.abspath(__file__))

    # 配置文件路径
    config_file = os.path.join(current_dir, 'config.yaml')
    init_config(config_file)
    return my_global_config
